<?php
namespace app\admin\controller;
class Ad extends Action
{
    public function index()
    {        
        $list = db("type")->where('cid',12)->order('vieworder asc')->select();
        $cid = input('cid');
        $title = input('title');
        return $this->fetch('index',['list'=>$list,'cid'=>$cid,'title'=>$title]);
    }
    public function lists(){
        $where = " a.id>0 ";
        $cid = input('cid');
        $title = input('title');
        if($cid) $where .= " and a.cid=".$cid;
        if($title) $where .= " and a.name like '%".$title."%' ";
        $pagesize = input('pagesize');
        $pages = input('pages');
        if($pages<=1)
        $a = 0;
        else
        $a = $pagesize*($pages-1);
        $total = db("ad")->alias("a")->where($where)->count();
        $totalpage = ceil($total/$pagesize);
        $data = db("ad")->alias("a")
                ->join('type b','a.cid =b.id','LEFT')
                ->field('a.id,a.name,FROM_UNIXTIME(a.addtime,"%Y年%m月%d日") as addtime,b.name as cname,a.vieworder,a.url')
                ->where($where)
                ->order("a.addtime","asc")
                ->limit($a,$pagesize)
                ->select();
        //输出Json数据
        return json(['code'=>1,'message'=>"获取数据成功",'data'=>$data,'totalpage'=>$totalpage]);
    }
    public function add(){
        if(request()->isPost()){
            $data["name"] = input("name");
            $data["cid"] = input("cid");
            $data["vieworder"] = input("vieworder");
            $data["addname"] = input("addname");
            $data["url"] = input("url");
            $data["m_url"] =input("m_url");
            $data["addtime"] = time();
            if(request()->file('imgpath')){
                $data["imgpath"] = model("Upload")->aupload(request()->file('imgpath'));
            }
            $save = db('ad')->insert($data);
            if($save){               
               return backUrl();
            }
        }
        $list = db("type")->where('cid',12)->order('vieworder asc')->select();
        return $this->fetch('add',['list'=>$list]);
    }
    public function edit(){
            $id = input("id");
            if(request()->isPost()){
                $data["name"] = input("name");
                $data["cid"] = input("cid");
                $data["vieworder"] = input("vieworder");
                $data["addname"] = input("addname");
                $data["addtime"] = time();
                $data["url"] =input("url");
                $data["m_url"] =input("m_url");
                if(request()->file('imgpath')){
                    @unlink("./".input('imgtext'));
                    $data["imgpath"] = model("Upload")->aupload(request()->file('imgpath'));
                }else{
                    $data["imgpath"] = input('imgtext');
                }
                $update = db("ad")->where('id',"=",$id)->update($data);
                if($update!==false){
                  return backUrl();
                }
            }
            $data = db("ad")->where('id',$id)->find();
            $list = db("type")->where('cid',12)->order('vieworder asc')->select();
            return $this->fetch('edit',['data'=>$data,'list'=>$list]);
    }
    public function delete(){
        $id=input('id');
        $data = db("ad")->where('id',$id)->find();
        if(is_file($data["imgpath"])) @unlink("./".$data["imgpath"]);  //删除缩略图
        $del = db("ad")->where("id",$id)->delete();
        if($del){
            return 1;
        }else{            
            return 0;
        }
    }
}